﻿
Partial Class Pages_Admin_fritid
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Listar upp nyheterna
        rptSchoolSpareTimeAdmin.DataSource = New NewsDAO().GetAllNewsSparetime()
        rptSchoolSpareTimeAdmin.DataBind()




    End Sub

    Protected Sub rptSchoolSpareTimeAdmin_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptSchoolSpareTimeAdmin.ItemCommand
        If e.Item.ItemType = ListItemType.Item Then

            For Each item In rptSchoolSpareTimeAdmin.Items
            Next
            'hittar hidden control med gömt id som skall användas att updatera rätt nyhet

            'hittar newsID
            Dim hiddenTxt As Label = CType(e.Item.FindControl("txtHidden"), Label)
            Dim id As Integer = CInt(hiddenTxt.Text)


            If (e.CommandArgument) Is "btnEdit" Then


                Dim c As News = New NewsDAO().getOneNews(id)
                lblHiddenCK.Text = CStr(id)
                CKEditor1.Text = c.Texten

                chkFrontNews.Checked = c.StartPage
                chkPrio.Checked = c.Prio

            End If

        End If

    End Sub

    Protected Sub btnDeleteNews_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteNews.Click
        If Not lblHiddenCK.Text = "" Then
            Dim id As Integer = CInt(lblHiddenCK.Text)

            Dim a As New NewsDAO
            a.deleteNewsByID(id)


            Response.Redirect("start.aspx")

        Else

            Response.Write("<script language='javascript'>alert('ingen nyhet är vald')</script>")

        End If
    End Sub

    Protected Sub btnSubmitNews_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitNews.Click
        Dim c As New News

        'skola & jobb sektion
        c.NewsSectionID = 1


        '    'checkboxarna ikryssade?
        If chkFrontNews.Checked = True Then
            c.StartPage = True
        Else
            c.StartPage = False
        End If
        If chkPrio.Checked = True Then
            c.Prio = True
        Else
            c.Prio = False
        End If

        'ny nyhet eller uppdatera gammal?
        If c.NewsSectionID <> 0 And Not lblHiddenCK.Text = "" Then
            Dim ost As Integer = CInt(lblHiddenCK.Text)

            c.NewsID = ost

            c.Texten = CKEditor1.Text

            Dim ab As New NewsDAO()
            ab.EditNews(c)
            Response.Write("<script language='javascript'>alert('Nyhet Updaterad')</script>")
            Response.Redirect("fritid.aspx")
        ElseIf c.NewsSectionID = 0 Then

            lblSection.ForeColor = Drawing.Color.Red
            lblSection.Text = "Välj sektion"


        ElseIf c.NewsSectionID <> 0 And c.NewsID = 0 Then

            c.Texten = CKEditor1.Text
            Dim ab As New NewsDAO()
            ab.createNewNews(c)
            Response.Write("<script language='javascript'>alert('Nyhet skapad')</script>")
            Response.Redirect("fritid.aspx")
        Else
            lblSection.ForeColor = Drawing.Color.Red
            lblSection.Text = "Välj sektion"


        End If
    End Sub

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Response.Redirect("Fritid.aspx")
    End Sub

    Protected Sub rptSchoolSpareTimeAdmin_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptSchoolSpareTimeAdmin.ItemDataBound
        'Hittar och databinder alla textboxarna och checkboxar
        If e.Item.ItemType = ListItemType.Item Then


            'header
            For Each item In rptSchoolSpareTimeAdmin.Items
            Next
            'Dim headerTxt As TextBox = CType(e.Item.FindControl("txtEditHeader"), TextBox)
            'headerTxt.Text = CStr(DataBinder.Eval(e.Item.DataItem, "header"))
            'text
            'Dim textTxt As TextBox = CType(e.Item.FindControl("txtEditText"), TextBox)
            'textTxt.Text = CStr(DataBinder.Eval(e.Item.DataItem, "texten"))

            'hidden ID
            Dim hiddenTxt As Label = CType(e.Item.FindControl("txtHidden"), Label)
            hiddenTxt.Text = CStr(CInt(DataBinder.Eval(e.Item.DataItem, "newsID")))
            'chkbox startpage ikryssad?
            Dim checkBoxStartPage As CheckBox = CType(e.Item.FindControl("chkFirstPage"), CheckBox)
            checkBoxStartPage.Checked = CBool(DataBinder.Eval(e.Item.DataItem, "StartPage"))
            'checkbox prio ikryssad?
            Dim checkBoxPrio As CheckBox = CType(e.Item.FindControl("chkPrioNews"), CheckBox)
            checkBoxPrio.Checked = CBool(DataBinder.Eval(e.Item.DataItem, "Prio"))
        End If
    End Sub
End Class
